package cn.lingyangwl.agile.infra.datamanage.controller;

import cn.lingyangwl.agile.infra.*;
import cn.lingyangwl.agile.infra.datamanage.mode.rqrs.*;
import cn.lingyangwl.agile.infra.datamanage.service.*;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.*;
import cn.lingyangwl.agile.model.enums.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.validation.group.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 数据集
 *
 * @author shenguangyang
 */
@Api(tags = "数据集")
@RestController
@RequestMapping("/dataset")

public class DatasetController {
    @Resource
    private DatasetService datasetService;
    @Resource
    private InfraApi infraApi;

    @ApiOperation(value = "分页数据集")
    @GetMapping("/page")
    public Resp<IPage<DatasetResp>> pageDataset(@Validated DatasetQueryReq req) {
        return Resp.ok(datasetService.pageDataset(req));
    }

    @ApiOperation(value = "通过id查找数据集")
    @GetMapping("/get")
    public Resp<DatasetResp> getDataset(@RequestParam(value = "id") Long id) {
        return Resp.ok(datasetService.getDataset(id));
    }


    @ApiOperation(value = "保存数据集")
    @PostMapping("/save")
    public Resp<?> saveDataset(@Validated(InsertGroup.class) @RequestBody DatasetSaveReq req) {
        return Resp.ok(datasetService.saveDataset(req));
    }

    @ApiOperation(value = "更新数据集")
    @PostMapping("/update")
    public Resp<?> updateDataset(@Validated(UpdateGroup.class) @RequestBody DatasetSaveReq req) {
        return Resp.ok(datasetService.updateDataset(req));
    }

    @ApiOperation(value = "批量删除数据集")
    @DeleteMapping("/batchDelete")
    public Resp<?> batchDeleteDataset(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(datasetService.batchDeleteDataset(ids));
    }

    @ApiOperation(value = "删除数据集")
    @DeleteMapping("/delete")
    public Resp<?> deleteDataset(@RequestParam(value = "id") Long id) {
        return Resp.ok(datasetService.deleteDataset(id));
    }

    @ApiOperation(value = "删除数据集组")
    @DeleteMapping("/deleteGroup")
    @RamResource(action = "onlineDev:dataset:deleteGroup", name = "删除数据集组", type = ActionType.WRITE)
    public Resp<?> deleteGroup(@RequestParam(value = "id") Long id) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(DataGroupBizTypeEnum.DATASET.getType());
        req.setIds(Collections.singletonList(id));
        return Resp.ok(datasetService.deleteGroup(req));
    }

    @ApiOperation(value = "批量删除数据集组")
    @DeleteMapping("/batchDeleteGroup")
    @RamResource(action = "onlineDev:dataset:deleteGroup", name = "删除数据集组", type = ActionType.WRITE)
    public Resp<?> batchDeleteGroup(@RequestParam(value = "ids") List<Long> ids) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(DataGroupBizTypeEnum.DATASET.getType());
        req.setIds(ids);
        return Resp.ok(datasetService.deleteGroup(req));
    }

    @ApiOperation(value = "保存数据集组")
    @PostMapping("/saveGroup")
    @RamResource(action = "onlineDev:dataset:saveGroup", name = "保存数据集组", type = ActionType.WRITE)
    public Resp<Long> saveDataGroup(@RequestBody DataGroupSaveReq req) {
        req.setBizType(DataGroupBizTypeEnum.DATASET.getType());
        return Resp.ok(infraApi.saveDataGroup(req));
    }

    @ApiOperation(value = "更新数据集组")
    @PostMapping("/updateGroup")
    @RamResource(action = "onlineDev:dataset:updateGroup", name = "更新数据集组", type = ActionType.WRITE)
    public Resp<?> updateDataGroup(@RequestBody DataGroupUpdateReq req) {
        return Resp.ok(infraApi.updateDataGroup(req));
    }
    @ApiOperation(value = "导入表")
    @PostMapping("/importTable")
    @RamResource(action = "onlineDev:dataset:importTable", name = "导入数据库表到数据集中", type = ActionType.WRITE)
    public Resp<?> importTable(@RequestBody ImportDatasetTableReq req) {
        datasetService.importTable(req);
        return Resp.ok();
    }
}
